Public Class frmTheCongNo1
    Private dataNhaCungCap, dataKhachHang, dataNVBH As DataTable
    Private sqlQuery As String
    Private Sub loadNhaCungCap()
        Try
            Dim sqlQuery As String = "SELECT ma_nhacungcap as ma , ten_nhacungcap as ten FROM tbl_nhacungcap ORDER BY ten_nhacungcap"
            dataNhaCungCap = moduleDatabaseConn.GetDataTable(sqlQuery)
            mcbNhaCungCap.Items.Clear()
            mcbNhaCungCap.LoadingType = MTGCComboBox.CaricamentoCombo.DataTable
            mcbNhaCungCap.ColumnNum = 2
            mcbNhaCungCap.ColumnWidth = "102;347"
            mcbNhaCungCap.HighlightBorderColor = Color.Black
            mcbNhaCungCap.GridLineHorizontal = True
            mcbNhaCungCap.GridLineVertical = True
            mcbNhaCungCap.ItemHeight = 16
            mcbNhaCungCap.DropDownHeight = 260
            mcbNhaCungCap.SourceDataTable = dataNhaCungCap
            mcbNhaCungCap.SelectedIndex = 0
        Catch ex As Exception
        End Try

    End Sub

    Private Sub loadKhachHang()
        Try
            Dim sqlQuery As String = "SELECT ma_nhacungcap as ma, ten_nhacungcap as ten FROM tbl_khachhang WHERE ma_nhom <> 'KT' ORDER BY ten_nhacungcap "
            dataKhachHang = moduleDatabaseConn.GetDataTable(sqlQuery)
            mcbNhaCungCap.Items.Clear()
            mcbNhaCungCap.LoadingType = MTGCComboBox.CaricamentoCombo.DataTable
            mcbNhaCungCap.ColumnNum = 2
            mcbNhaCungCap.ColumnWidth = "102;347"
            mcbNhaCungCap.HighlightBorderColor = Color.Black
            mcbNhaCungCap.GridLineHorizontal = True
            mcbNhaCungCap.GridLineVertical = True
            mcbNhaCungCap.ItemHeight = 16
            mcbNhaCungCap.DropDownHeight = 260
            mcbNhaCungCap.SourceDataTable = dataKhachHang
            mcbNhaCungCap.SelectedIndex = 0
        Catch ex As Exception
        End Try
    End Sub
    Private Sub loadNVBH()
        Try
            Dim sqlQuery As String = "SELECT ma, ten FROM tbl_nhanvienbanhang  ORDER BY ten "
            dataNVBH = moduleDatabaseConn.GetDataTable(sqlQuery)
            mcbNhaCungCap.Items.Clear()
            mcbNhaCungCap.LoadingType = MTGCComboBox.CaricamentoCombo.DataTable
            mcbNhaCungCap.ColumnNum = 2
            mcbNhaCungCap.ColumnWidth = "102;347"
            mcbNhaCungCap.HighlightBorderColor = Color.Black
            mcbNhaCungCap.GridLineHorizontal = True
            mcbNhaCungCap.GridLineVertical = True
            mcbNhaCungCap.ItemHeight = 16
            mcbNhaCungCap.DropDownHeight = 260
            mcbNhaCungCap.SourceDataTable = dataNVBH
            mcbNhaCungCap.SelectedIndex = 0
        Catch ex As Exception
        End Try
    End Sub
    Private Sub frmTheCongNo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        moduleUtils.setDinhDangVietNam()
        Me.DPTTuNgay.Text = moduleUtils.getNgayDauThang(Today.Year, Today.Month)
        Me.DPTDenNgay.Text = moduleUtils.getNgayCuoiThang(Today.Year, Today.Month)
        Me.Alert_Max.Checked = True
        Me.Alert_Min.Checked = False
        Me.gridViewSearch1.Visible = False
    End Sub
    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
        Me.Dispose()
    End Sub
    Private Sub btnInBaoCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInBaoCao.Click
        If Alert_Max.Checked = True Then
            congno.tinhTheCongNo_NhaCungCap(CDate(Me.DPTTuNgay.Text.Trim), CDate(Me.DPTDenNgay.Text.Trim), Me.mcbNhaCungCap.Text.Trim, 2)
            Dim myForm As New FrmCrTheCongNo_NhaCungCap
            myForm.setTenncc(Me.txtTenNhaCungCap.Text)
            myForm.setThangnam(CDate(Me.DPTTuNgay.Text))
            myForm.setTungay(CDate(Me.DPTTuNgay.Text))
            myForm.setDenngay(CDate(Me.DPTDenNgay.Text))
            myForm.Show()
        End If
        If The_NVBH.Checked = True Then
            congno.tinhTheCongNo_Nhanvienbanhang(CDate(Me.DPTTuNgay.Text.Trim), CDate(Me.DPTDenNgay.Text.Trim), Me.mcbNhaCungCap.Text.Trim, 3)
            Dim myForm As New FrmCrTheCongNo_Nhanvienbanhang
            myForm.setTenkh(Me.txtTenNhaCungCap.Text)
            myForm.setThangnam(CDate(Me.DPTTuNgay.Text))
            myForm.setTungay(CDate(Me.DPTTuNgay.Text))
            myForm.setDenngay(CDate(Me.DPTDenNgay.Text))
            myForm.Show()
        End If
        If Alert_Min.Checked = True Then
            congno.tinhTheCongNo_KhachHang(CDate(Me.DPTTuNgay.Text.Trim), CDate(Me.DPTDenNgay.Text.Trim), Me.mcbNhaCungCap.Text.Trim, 3)
            Dim myForm As New FrmCrTheCongNo_KhachHang
            myForm.setTenkh(Me.txtTenNhaCungCap.Text)
            myForm.setThangnam(CDate(Me.DPTTuNgay.Text))
            myForm.setTungay(CDate(Me.DPTTuNgay.Text))
            myForm.setDenngay(CDate(Me.DPTDenNgay.Text))
            myForm.Show()
        End If
        If Me.Rbt_CN_KH_CHITIET.Checked = True Then

            congno.tinhTheCongNo_KhachHang_ChiTiet(CDate(Me.DPTTuNgay.Text.Trim), CDate(Me.DPTDenNgay.Text.Trim), Me.mcbNhaCungCap.Text.Trim, 3)
            'Tinh toan lai cot NOCUOI,tinh tren Luoi ,sau do update nguoc lai coso dulieu
            sqlQuery = "SELECT * FROM tbl_dot_thecongno_khachhang_chitiet ORDER BY ngay_chungtu,id"
            Me.Grid1.DataSource = moduleDatabaseConn.GetDataTable(sqlQuery)
            For i As Integer = 1 To Grid1.Rows.Count - 1
                Me.Grid1.Item(Me.colnocuoi.Index, i).Value = Me.Grid1.Item(Me.colnocuoi.Index, i - 1).Value + Me.Grid1.Item(Me.colpsno.Index, i).Value - Me.Grid1.Item(Me.colpsco.Index, i).Value
            Next
            For i As Integer = 0 To Grid1.Rows.Count - 1
                sqlQuery = "UPDATE tbl_dot_thecongno_khachhang_chitiet "
                sqlQuery += " SET"
                sqlQuery += " nocuoi = " & Replace(Me.Grid1.Item(Me.colnocuoi.Index, i).Value, ",", ".")
                sqlQuery += " WHERE ID = '" & Me.Grid1.Item(Me.colID.Index, i).Value & "'"
                moduleDatabaseConn.ExecuteNonQuery(sqlQuery)
            Next
            'het tinh toan NOCUOI
            Dim myForm As New FrmCrTheCongNo_KhachHang_chitiet
            myForm.setTenkh(Me.txtTenNhaCungCap.Text)
            myForm.setThangnam(CDate(Me.DPTTuNgay.Text))
            myForm.setTungay(CDate(Me.DPTTuNgay.Text))
            myForm.setDenngay(CDate(Me.DPTDenNgay.Text))
            myForm.Show()
        End If
    End Sub
    Private Sub Alert_Max_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Alert_Max.CheckedChanged
        Me.mcbNhaCungCap.Text = ""
        Me.txtTenNhaCungCap.Text = " "
        Me.gridViewSearch1.Visible = False
        loadNhaCungCap()
    End Sub
    Private Sub The_NVBH_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles The_NVBH.CheckedChanged
        Me.mcbNhaCungCap.Text = ""
        Me.txtTenNhaCungCap.Text = " "
        Me.gridViewSearch1.Visible = False
        loadNVBH()
    End Sub

    Private Sub Alert_Min_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Alert_Min.CheckedChanged
        Me.mcbNhaCungCap.Text = ""
        Me.txtTenNhaCungCap.Text = " "
        Me.gridViewSearch1.Visible = False
        loadKhachHang()
    End Sub
    Private Sub Rbt_CN_KH_CHITIET_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Rbt_CN_KH_CHITIET.CheckedChanged
        Me.mcbNhaCungCap.Text = ""
        Me.txtTenNhaCungCap.Text = " "
        Me.gridViewSearch1.Visible = False
        loadKhachHang()
    End Sub
    Private Sub mcbNhaCungCap_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles mcbNhaCungCap.SelectedIndexChanged
        If mcbNhaCungCap.Text <> "" Then
            Me.txtTenNhaCungCap.Text = Me.mcbNhaCungCap.SelectedItem.Col2
        Else
            Me.txtTenNhaCungCap.Text = " "
        End If
    End Sub
    Private Sub txtTenNhaCungCap_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTenNhaCungCap.Click
        Me.txtTenNhaCungCap.Text = " "
        Me.gridViewSearch1.Visible = True
    End Sub
    Private Sub txtTenNhaCungCap_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTenNhaCungCap.TextChanged
        If Alert_Max.Checked = True Then
            Dim view As DataView = New DataView(dataNhaCungCap)
            view.RowFilter = " ma like '%" & Me.txtTenNhaCungCap.Text.Trim & "%' or ten like '%" & Me.txtTenNhaCungCap.Text.Trim & "%' "
            Me.gridViewSearch1.DataSource = view
        End If
        If The_NVBH.Checked = True Then
            Dim view As DataView = New DataView(dataNVBH)
            view.RowFilter = " ma like '%" & Me.txtTenNhaCungCap.Text.Trim & "%' or ten like '%" & Me.txtTenNhaCungCap.Text.Trim & "%' "
            Me.gridViewSearch1.DataSource = view
        End If
        If Alert_Min.Checked = True Then
            Dim view As DataView = New DataView(dataKhachHang)
            view.RowFilter = " ma like '%" & Me.txtTenNhaCungCap.Text.Trim & "%' or ten like '%" & Me.txtTenNhaCungCap.Text.Trim & "%' "
            Me.gridViewSearch1.DataSource = view
        End If
        If Rbt_CN_KH_CHITIET.Checked = True Then
            Dim view As DataView = New DataView(dataKhachHang)
            view.RowFilter = " ma like '%" & Me.txtTenNhaCungCap.Text.Trim & "%' or ten like '%" & Me.txtTenNhaCungCap.Text.Trim & "%' "
            Me.gridViewSearch1.DataSource = view
        End If
        
    End Sub
    Private Sub gridViewSearch1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridViewSearch1.CellClick
        If Alert_Max.Checked = True Then
            Dim row1 As Integer = Me.gridViewSearch1.CurrentRow.Index
            moduleUtils.setIndexComBo(Me.gridViewSearch1.Item(Me.colMa1.Index, row1).Value, Me.mcbNhaCungCap, dataNhaCungCap)
            Me.txtTenNhaCungCap.Text = mcbNhaCungCap.SelectedItem.Col2
            Me.gridViewSearch1.Visible = False
        End If
        If The_NVBH.Checked = True Then
            Dim row2 As Integer = Me.gridViewSearch1.CurrentRow.Index
            moduleUtils.setIndexComBo(Me.gridViewSearch1.Item(Me.colMa1.Index, row2).Value, Me.mcbNhaCungCap, dataNVBH)
            Me.txtTenNhaCungCap.Text = mcbNhaCungCap.SelectedItem.Col2
            Me.gridViewSearch1.Visible = False
        End If
        If Alert_Min.Checked = True Then
            Dim row2 As Integer = Me.gridViewSearch1.CurrentRow.Index
            moduleUtils.setIndexComBo(Me.gridViewSearch1.Item(Me.colMa1.Index, row2).Value, Me.mcbNhaCungCap, dataKhachHang)
            Me.txtTenNhaCungCap.Text = mcbNhaCungCap.SelectedItem.Col2
            Me.gridViewSearch1.Visible = False
        End If
        If Rbt_CN_KH_CHITIET.Checked = True Then
            Dim row2 As Integer = Me.gridViewSearch1.CurrentRow.Index
            moduleUtils.setIndexComBo(Me.gridViewSearch1.Item(Me.colMa1.Index, row2).Value, Me.mcbNhaCungCap, dataKhachHang)
            Me.txtTenNhaCungCap.Text = mcbNhaCungCap.SelectedItem.Col2
            Me.gridViewSearch1.Visible = False
        End If
    End Sub
End Class